
import Koa  from 'koa'
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')

import mongoose from 'mongoose'
// 解决post 参数的请求
import bodyParser from 'koa-bodyparser'
//  帮忙操作cookie
import session from 'koa-generic-session'
import Redis from 'koa-redis'
// 美化json
import json from 'koa-json'
// 配置文件
import dbConfig from './dbs/config'
// 处理登录相关
import passport from './interface/utils/passport'
// 引入接口路由
import users from './interface/users'
import geo from './interface/geo'
import search from './interface/search'
import categroy from './interface/categroy'
import cart from './interface/cart'
import order from './interface/order'

const app = new Koa()
const host = process.env.HOST || '127.0.0.1'
const port = process.env.PORT || 3000



app.proxy=true
const store=Redis({
    host:'111.230.112.121',
    password:'lsj@2019',
    port:'6379'
})
// key是键值名
// 连接session和redis
// 左边是键值名
app.keys=['mt','keyskeys']
app.use(session({
  key:'mt',
  prefix:'mt:uid',
  // store:new Redis()
  store:store
}))

app.use(bodyParser({
  extendTypes:['json','from','text']
}))


app.use(json())

// 连接数据库
mongoose.connect(dbConfig.dbs,{
  useNewUrlParser:true
})

app.use(passport.initialize())
app.use(passport.session())

// Import and Set Nuxt.js options
let config = require('../nuxt.config.js')
config.dev = !(app.env === 'production')

async function start() {
  // Instantiate nuxt.js
  const nuxt = new Nuxt(config)

  // Build in development
  if (config.dev) {
    const builder = new Builder(nuxt)
    await builder.build()
  }

//获取所有的路由表 
  app.use(users.routes()).use(users.allowedMethods())
  app.use(geo.routes()).use(geo.allowedMethods())
  app.use(search.routes()).use(search.allowedMethods())
  app.use(categroy.routes()).use(categroy.allowedMethods())
  app.use(cart.routes()).use(cart.allowedMethods())
  app.use(order.routes()).use(order.allowedMethods())
  
  app.use(ctx => {
    ctx.status = 200 // koa defaults to 404 when it sees that status is unset

    return new Promise((resolve, reject) => {
      ctx.res.on('close', resolve)
      ctx.res.on('finish', resolve)
      nuxt.render(ctx.req, ctx.res, promise => {
        // nuxt.render passes a rejected promise into callback on error.
        promise.then(resolve).catch(reject)
      })
    })
  })

  app.listen(port, host)
  consola.ready({
    message: `Server listening on http://${host}:${port}`,
    badge: true
  })
}

start()
